package zhaoq_hero.superapp;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

import zhaoq_hero.superapp.providers.DataContract;

public class HistoryActivity extends AppCompatActivity implements AdapterView.OnItemClickListener  {

    private ListView listView;
    private List<String> list;
    private ArrayAdapter adapter;
    private ContentResolver resolver;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_history);

        listView = (ListView) findViewById(R.id.history_listview);
        resolver = getContentResolver();
        list = new LinkedList<>();
        Cursor cursor;

        type = getIntent().getStringExtra("type");
        if (type.equals("history")) {
            setTitle("历史记录");
            cursor = resolver.query(DataContract.History.CONTENT_URI,
                    null,
                    null,
                    null,
                    null);
            if (cursor.getCount() != 0) {
                while (cursor.moveToNext()) {
                    String s = cursor.getInt(0) + " " + cursor.getString(1) + " " + cursor.getString(2) + " " + cursor.getString(3);
                    list.add(s);
                }
            }
        } else {
            setTitle("书签");
            cursor = resolver.query(DataContract.BookMark.BOOK_URI,
                    null,
                    null,
                    null,
                    null);
            if (cursor.getCount() != 0) {
                while (cursor.moveToNext()) {
                    String s = cursor.getInt(0) + " " + cursor.getString(1) + " " + cursor.getString(2);
                    list.add(s);
                }
            }
        }

        if (list != null) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(this);
            listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                @Override
                public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                    menu.add(0, 0, 0, "删除");
                    menu.add(0, 1, 0, "删除所有");
                }
            });
        }


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = getIntent();
        intent.putExtra("url", list.get(position).split(" ")[1]);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info;
        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        View view = info.targetView;
        ContentResolver resolver = getContentResolver();
        TextView tx = (TextView) view.findViewById(android.R.id.text1);
        switch (item.getItemId()) {
            case 0:
                int delete;
                if (type.equals("history")) {
                    delete = resolver.delete(DataContract.History.CONTENT_URI,
                            DataContract.History._ID + " = " + tx.getText().toString().split(" ")[0],
                            null);
                }else{
                    delete = resolver.delete(DataContract.BookMark.BOOK_URI,
                            DataContract.History._ID + " = " + tx.getText().toString().split(" ")[0],
                            null);
                }
                if (delete != -1) {
                    list.remove(info.position);
                    Toast.makeText(HistoryActivity.this, "已删除指定的" + type, Toast.LENGTH_SHORT).show();
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1:
                int deleteAll ;
                if(type.equals("history")){
                    deleteAll= resolver.delete(DataContract.History.CONTENT_URI,
                            null,
                            null);
                }else{
                    deleteAll= resolver.delete(DataContract.BookMark.BOOK_URI,
                            null,
                            null);
                }
                if (deleteAll != -1) {
                    list.clear();
                    Toast.makeText(HistoryActivity.this, "已删除所有的" + type, Toast.LENGTH_SHORT).show();
                    adapter.notifyDataSetChanged();
                }
                break;
        }
        return false;
    }
}
